﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iemtm.WeChat.Core.Extension
{
    public static class StringExtensions
    {
        /// <summary>
        /// 将字符串数组 拼接成 字符串
        /// </summary>
        /// <param name="ls"></param>
        /// <param name="splitter">拼接符号</param>
        /// <returns></returns>
        public static string StrListToStr(this List<string> ls, string splitter)
        {
            StringBuilder Ts = new StringBuilder();
            foreach(var l in ls)
            {
                Ts.Append(l + splitter);
            }
            //去掉最后面多次出来的 splitter
            return Ts.ToString().Substring(0, Ts.Length - splitter.Length);

        }
        /// <summary>
        /// 字符串分割
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="strSplit"></param>
        /// <returns></returns>
        public static string[] StringSplitTirm(this string strSource, string strSplit)
        {
            string[] stt = StringSplit(strSource, strSplit);
            List<string> SS = new List<string>();
            foreach (var i in stt)
            {
                if (!string.IsNullOrEmpty(i))
                {
                    SS.Add(i);
                }
            }
            return SS.ToArray();
        }
        /// <summary>
        /// 将字符串分割成数组
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="strSplit"></param>
        /// <returns></returns>
        private static string[] StringSplit(string strSource, string strSplit)
        {
            string[] strtmp = new string[1];
            int index = strSource.IndexOf(strSplit, 0);
            if (index < 0)
            {
                strtmp[0] = strSource;
                return strtmp;
            }
            else
            {
                strtmp[0] = strSource.Substring(0, index);
                return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
            }
        }

        /// <summary>
        /// 采用递归将字符串分割成数组
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="strSplit"></param>
        /// <param name="attachArray"></param>
        /// <returns></returns>
        private static string[] StringSplit(string strSource, string strSplit, string[] attachArray)
        {
            string[] strtmp = new string[attachArray.Length + 1];
            attachArray.CopyTo(strtmp, 0);

            int index = strSource.IndexOf(strSplit, 0);
            if (index < 0)
            {
                strtmp[attachArray.Length] = strSource;
                return strtmp;
            }
            else
            {
                strtmp[attachArray.Length] = strSource.Substring(0, index);
                return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
            }
        }


        /// <summary>
        /// 将Unicode编码转换为汉字字符串
        /// </summary>
        /// <param name="str">Unicode编码字符串</param>
        /// <returns>汉字字符串</returns>
        public static string ToGB2312(this string str)
        {
            string r = "";
            MatchCollection mc = Regex.Matches(str, @"([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            byte[] bts = new byte[2];
            foreach (Match m in mc)
            {
                bts[0] = (byte)int.Parse(m.Groups[2].Value, System.Globalization.NumberStyles.HexNumber);
                bts[1] = (byte)int.Parse(m.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
                r += Encoding.Unicode.GetString(bts);
            }
            return r;
        }
    }
}
